package com.yxzr.youyou.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yxzr.youyou.R;
import com.yxzr.youyou.bean.RechargeBean;

import java.util.ArrayList;
import java.util.List;

public class RechargeAdapter extends RecyclerView.Adapter<RechargeAdapter.ViewHolder> {
    private Context mContext;
    private OnClickListener onClickListener;
    private int selectIndex = 0;
    private List<RechargeBean.DataBean> list = new ArrayList<>();
    public RechargeAdapter(Context context){
        mContext = context;
    }
    public void addList(List<RechargeBean.DataBean> mlist){
        if (mlist != null && mlist.size() > 0){
            list.addAll(mlist);
            notifyDataSetChanged();
        }
    }

    public void clearList(){
        list.clear();
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.recharge_adapter_layout,parent,false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        if (selectIndex == position){
            holder.recharge_linear.setBackgroundResource(R.drawable.recharge_rv_select);
        }else {
            holder.recharge_linear.setBackgroundResource(R.drawable.recharge_rv_noselect);
        }

        holder.recharge_day.setText(list.get(position).getDay());
        holder.recharge_money.setText(list.get(position).getPrice());
        if (TextUtils.isEmpty(list.get(position).getGive_day()) || "0".equals(list.get(position).getGive_day())){
            holder.recharge_sendday.setVisibility(View.INVISIBLE);
        }else {
            holder.recharge_sendday.setVisibility(View.VISIBLE);
            holder.recharge_sendday.setText("(赠送"+list.get(position).getGive_day()+"天)");
        }


        holder.recharge_yuanjia.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG ); //中间横线
        holder.recharge_yuanjia.getPaint().setAntiAlias(true);// 抗锯齿
        holder.recharge_yuanjia.setText("¥"+list.get(position).getOld_price());

        holder.recharge_linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickListener.onClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout recharge_linear;
        private TextView recharge_day,recharge_money,recharge_yuanjia,recharge_sendday;
        public ViewHolder(View itemView) {
            super(itemView);
            recharge_day = itemView.findViewById(R.id.recharge_day);
            recharge_money = itemView.findViewById(R.id.recharge_money);
            recharge_yuanjia = itemView.findViewById(R.id.recharge_yuanjia);
            recharge_sendday = itemView.findViewById(R.id.recharge_sendday);
            recharge_linear = itemView.findViewById(R.id.recharge_linear);
        }
    }

    public void setSelectIndex(int position){
        this.selectIndex = position;
        notifyDataSetChanged();
    }

    public interface OnClickListener{
        void onClick(int position);
    }

    public void setOnClickListener(OnClickListener o){
        this.onClickListener = o;
    }
}
